/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserMemberLevelPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserMemberLevelPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserMemberLevelSqlProvider {
	public static final String TABLE_NAME = "user_member_level";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userMemberLevel
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserMemberLevels(final UserMemberLevelPo userMemberLevel) {
		return new CustomerSQL() {{
			SELECT("create_time,creator_id,description,id,level_name,score_max,score_min,summary,update_time,updater_id ");
			FROM(TABLE_NAME);
			
			if (null != userMemberLevel.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getLevelName())) {
				WHERE("level_name=#{levelName}");
			}
			if (null != userMemberLevel.getScoreMax()) {
				WHERE("score_max=#{scoreMax}");
			}
			if (null != userMemberLevel.getScoreMin()) {
				WHERE("score_min=#{scoreMin}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getSummary())) {
				WHERE("summary=#{summary}");
			}
			if (null != userMemberLevel.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if(StringUtils.isNotBlank(userMemberLevel.getSortField())) {
				ORDER_BY(userMemberLevel.getSortField() + (StringUtils.isNotBlank(userMemberLevel.getSortMethod()) ? "desc" : userMemberLevel.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userMemberLevel.getStart() && null != userMemberLevel.getSize()) {
				LIMIT(userMemberLevel.getStart(), userMemberLevel.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userMemberLevel
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserMemberLevelCount(final UserMemberLevelPo userMemberLevel) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != userMemberLevel.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getLevelName())) {
				WHERE("level_name=#{levelName}");
			}
			if (null != userMemberLevel.getScoreMax()) {
				WHERE("score_max=#{scoreMax}");
			}
			if (null != userMemberLevel.getScoreMin()) {
				WHERE("score_min=#{scoreMin}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getSummary())) {
				WHERE("summary=#{summary}");
			}
			if (null != userMemberLevel.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserMemberLevelById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userMemberLevel
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserMemberLevel(final UserMemberLevelPo userMemberLevel) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != userMemberLevel.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getDescription())) {
				VALUES("description","#{description}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getLevelName())) {
				VALUES("level_name","#{levelName}");
			}
			if (null != userMemberLevel.getScoreMax()) {
				VALUES("score_max","#{scoreMax}");
			}
			if (null != userMemberLevel.getScoreMin()) {
				VALUES("score_min","#{scoreMin}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getSummary())) {
				VALUES("summary","#{summary}");
			}
			if (null != userMemberLevel.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userMemberLevel
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserMemberLevel(final UserMemberLevelPo userMemberLevel) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != userMemberLevel.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getDescription())) {
				SET("description=#{description}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getLevelName())) {
				SET("level_name=#{levelName}");
			}
			if (null != userMemberLevel.getScoreMax()) {
				SET("score_max=#{scoreMax}");
			}
			if (null != userMemberLevel.getScoreMin()) {
				SET("score_min=#{scoreMin}");
			}
			if (StringUtils.isNotBlank(userMemberLevel.getSummary())) {
				SET("summary=#{summary}");
			}
			if (null != userMemberLevel.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserMemberLevel(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
